package com.twotiger.and.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DebtProjectdetailData;
import com.twotiger.and.bean.ProjectdetailData;
import com.twotiger.and.bean.RepaymentList;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;

public class ProjectRepaymentAdpter extends BaseAdapter {
    LayoutInflater inflater;
    private Basebean mdate;
    private Context context;
    private List<RepaymentList> mrepaylist;

    public ProjectRepaymentAdpter(Context context, Basebean date) {
        super();
        inflater = LayoutInflater.from(context);
        mdate = date;
        if (date instanceof ProjectdetailData) {
            mrepaylist = JSON.parseArray(((ProjectdetailData) mdate).getRepaymentList(),
                RepaymentList.class);
        } else if (date instanceof DebtProjectdetailData) {
            mrepaylist = JSON.parseArray(((DebtProjectdetailData) mdate).getRepaymentList(),
                RepaymentList.class);
        }
        this.context = context;
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mrepaylist)) {
            return mrepaylist.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MViewholder mViewHolder = null;
        if (convertView == null) {
            mViewHolder = new MViewholder();
            convertView = inflater.inflate(R.layout.project_detail_item2_list_item, null);
            mViewHolder.repay_time = (TextView) convertView.findViewById(R.id.item_text_1);
            mViewHolder.repay_type = (TextView) convertView.findViewById(R.id.item_text_2);
            mViewHolder.repay_amount = (TextView) convertView.findViewById(R.id.item_text_3);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MViewholder) convertView.getTag();
        }
        if (mdate instanceof ProjectdetailData) {
            RepaymentList repayment = mrepaylist.get(position);
            if (repayment.getStatus() == (41 | 51 | 62)) {
                mViewHolder.repay_time.setText(DateUtil.getDateStringForLong(
                    Long.valueOf(repayment.getRealTime()), "yyyy-MM-dd"));
                mViewHolder.repay_amount
                    .setText(ArithUtils.coverMoney(repayment.getRealCapital()
                                                   + repayment.getRealInterest() + ""));
                if ((repayment.getRealCapital() != 0) && (repayment.getRealInterest() != 0)) {
                    mViewHolder.repay_type.setText("本金+利息");
                } else if ((repayment.getRealCapital() == 0) && (repayment.getRealInterest() != 0)) {
                    mViewHolder.repay_type.setText("利息");
                } else if ((repayment.getRealCapital() != 0) && (repayment.getRealInterest() == 0)) {
                    mViewHolder.repay_type.setText("本金");
                }
            } else {
                mViewHolder.repay_time.setText(DateUtil.getDateStringForLong(
                    Long.valueOf(repayment.getOughtedTime()), "yyyy-MM-dd"));
                mViewHolder.repay_amount.setText(ArithUtils.coverMoney(repayment.getCapital()
                                                                       + repayment.getInterest()
                                                                       + ""));
                if ((repayment.getCapital() != 0) && (repayment.getInterest() != 0)) {
                    mViewHolder.repay_type.setText("本金+利息");
                } else if ((repayment.getCapital() == 0) && (repayment.getInterest() != 0)) {
                    mViewHolder.repay_type.setText("利息");
                } else if ((repayment.getCapital() != 0) && (repayment.getInterest() == 0)) {
                    mViewHolder.repay_type.setText("本金");
                }
            }
        } else if (mdate instanceof DebtProjectdetailData) {
            mViewHolder.repay_type.setVisibility(View.GONE);
            RepaymentList repayment = mrepaylist.get(position);
            mViewHolder.repay_time.setText(DateUtil.getDateStringForLong(
                Long.valueOf(repayment.getOughtedTime()), "yyyy-MM-dd"));
            mViewHolder.repay_amount.setText(repayment.getStatusName());
        }

        return convertView;
    }

    class MViewholder {

        public TextView repay_type;
        public TextView repay_amount;
        public TextView repay_time;

    }
}
